<%@ page language="java" contentType="text/html;"  
    pageEncoding="GBK"%>  
     <%
		response.setHeader("Location", request.getRequestURL().toString());
     System.out.println(request.getRequestURL().toString());
    String logout = (String)request.getParameter("logout");
    if(logout != null){
    	System.out.println("logging out!");
    	session.invalidate();
    	session = request.getSession(true);//refresh session object 
    	System.out.println("session:"+session.getAttribute("username"));
    	session.setAttribute("logout", true);
    }else{
    	if(session.getAttribute("username") != null){
    		%><jsp:forward page="success.jsp" /><%
    	}
    }
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />  
<title>Login</title>  

<link rel="stylesheet" href="/DBP/style/stylesheet.css" type="text/css" />  
<script type="text/javascript">  
    function check(){   
		// Clear success msgs
		document.getElementById("msgs_div").innerHTML = "";
		
        var form = document.getElementById("form1");
        if(form.username.value==""){
            document.getElementById("username_msg").style.display="inline";
            form.username.focus();   
            return false;   
        } else {
        	document.getElementById("username_msg").style.display="none";
        }
        if(form.password.value==""){   
            document.getElementById("password_msg").style.display="inline";
            form.password.focus();   
            return false;   
        } else {
        	document.getElementById("password_msg").style.display="none";
        }
        return true;
    }
    
    function resetPassword() {
		// Clear success msgs
		document.getElementById("msgs_div").innerHTML = "";
        
    	if (document.getElementById("usernameReset").value==""){
            document.getElementById("username_msg").style.display="inline"; 
           	return false;   
        }
		return true;
    }

	function addToHiddenField () {
		document.getElementById("usernameReset").value = document.getElementById("username").value;
		return true;
	}
    
    function AjaxHandler() {
    	//alert("AjaxHandler function run");
    }	
  
</script>   
</head>  
<body>  
<div class="bg">

<div class='content' >
	
	<div class=" title"><img src="/DBP/images/title2.png" alt="DBPOLL"/></div>

	<div class="entrytitle" id="loginleft">
		Welcome to DbPoll! 
		<div class="entry">
		<img src="images/ttech.png" style="width:150px;float:right; " alt="DBPOLL"/> <p style="float:clear;">DBpoll is a centralised survey service by Turning Technologies, featuring integration with ResponseCard Receivers blah blah lorem ipsum samuel l jackson etc <br/><br/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eget est eros, vitae porta magna. Nam magna est, molestie et gravida a, porttitor id elit. Proin feugiat nisl vel nisi aliquet ac blandit neque tincidunt. Nam tincidunt malesuada interdum. Proin quis orci quam, at mollis elit. Ut ac porta libero. Aenean vitae velit ut felis ullamcorper eleifend. Aliquam gravida bibendum enim non vehicula. Suspendisse volutpat tristique arcu eget tincidunt. Etiam venenatis commodo nibh at consequat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><br/> 
		</div></div>
		
		
		<div class="entrytitle" style="float:clear;">
			<p>Login</p>
			<div class="entry" >
		
			<form action="LoginServlet" style="display:inline" method="post" id="form1">
			<p display="inline">Username <input type="text" name="username" id="username" onkeyup="addToHiddenField();" /><div id="username_msg" class="error" >Please input a username</div>
			<%
				// Check for incorrect username submitted
				if (session.getAttribute("UsernameIncorrect") != null && session.getAttribute("UsernameIncorrect").equals("ttrue")) {
					out.println ("<div class=\"error\" style=\"display:inline\">The username submitted is incorrect!</div>");
				}
			 %>
			</p>
			<p display="inline">Password <input type="password" name="password" /><div id="password_msg" class="error">Please input a password</div>
			<%
				// Check for incorrect username submitted
				if (session.getAttribute("PasswordIncorrect") != null && session.getAttribute("PasswordIncorrect").equals("ttrue")) {
					out.println ("<div class=\"error\" style=\"display:inline\">The password submitted is incorrect!</div>");
				}
			 %>
			 </p>
			<input type="submit" name="submit" onclick="return check();" class="button" style="display:inline"/></form>
			<form action="resetServlet" style="display:inline" method="post" id="form2"><input type="submit" value="Reset Password" name="reset" style="display:inline" onclick="return resetPassword();" class="button"/><input type="text" name="usernameReset" style="visibility:hidden;" id="usernameReset" /> 
			</form>
			<div id="msgs_div">
			<%
				// Check for success/failure of reset username
				if (session.getAttribute("login_reset_success") != null && session.getAttribute("login_reset_success").equals("t")) {
					out.println("<div class=\"note\" style=\"display:block\" > Successfully reset password. New password has been emailed to you.</div>");
				}
				if (session.getAttribute("login_reset_noUser") != null && session.getAttribute("login_reset_noUser").equals("t")) {
					out.println("<div class=\"error\" style=\"display:block\" > Reset password failed.  Username does not exist.</div>");
				}
			%>
			</div>
			<div class="note" style="display:<%if(session.getAttribute("logout")!= null){out.write("block");session.removeAttribute("logout");}else{out.write("none");} %>">You have been successfully logged out.</div>
			</div>  
			</div>
		</div>
	</div>
</body>  
</html>  
